<?php

namespace App\Console\Commands;

use App\Http\Models\Mysql\SsyJstPurchaseinOrderTable;
use App\Http\Repository\LinmaoRepository;
use App\Http\Repository\WmsRepository;
use Illuminate\Console\Command;

class pullReturnsOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:pullReturnsOrder {--sourceCode=} {--sleep=} {--start=} {--end=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '拉取巨沃入库回货数据';

    /**
     * Create a new command instance.
     * /30 * * * *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $sleep = $this->option('sleep') ?: 0;
        $sourceCode = $this->option('sourceCode') ?: "";
        $start = $this->option('start') ?: "";
        $end = $this->option('end') ?: "";
        if($sleep){
            sleep((Int)$sleep);
        }
        $orderObj = (new SsyJstPurchaseinOrderTable())->orderBy('io_date', 'desc')->first(['io_date'])->toArray();
        $totalPage = 1;
        $limit = 20;
        $startDate = $start ?: date('Y-m-d H:i:s', strtotime($orderObj['io_date']. "+ 1 second"));
        $endDate = $end ?: date('Y-m-d H:i:s');
        for($page = 1; $page <= $totalPage; $page++){
            $res = WmsRepository::purchaseOrder($page, $limit, $startDate, $endDate, $sourceCode);
            if($page == 1){
                $totalPage = $res['data']['page'] ?? 1;
            }
            $list = $res['data']['headList'] ?? [];
            if(empty($list)){
                break;
            }
            $this->pushData($list, $page, $totalPage);
        }
    }

    public function pushData($list, $page, $totalPage)
    {
        $num = 1;
        foreach($list as $item){
            foreach ($item['detailList'] as $detail){
                try{
                    list($ioId, $skuId, $poId, $ioDate, $qty, $remark, $processingCode) = [$item['wmsCode']??"", $detail['sku']??"", $item['orderCode']??"",
                        $item['operuserDate']??"", $detail['qty']??"", $item['remark']??"", $item['sourceCode']??""];
                    echo "正在执行[$totalPage, $page], 第{$num}条：入库单号：{$ioId} sku：{$skuId} 采购单号：{$poId} 回货日期：{$ioDate}".
                        " 回货数：{$qty}\n";
                    $exist = (new SsyJstPurchaseinOrderTable())->where('io_id', $ioId)->where('sku_id', $skuId)->where('po_id', $poId)->where('io_date', $ioDate)
                        ->where('qty', $qty)->first();
                    if(!empty($exist)){
                        echo "已存在, 无需入库\n";
                    }else{
                        $wh_id = $item['warehouseCode']=="HZC" ? 3 : ($item['warehouseCode']=="HZCPC" ? 4 : $item['warehouseCode']);
                        $data = [
                            'io_id' => $ioId,
                            'po_id' => $poId,
                            'supplier_id' => $item['supplierCode'] ?? "",
                            'modified' => $ioDate,
                            'creator_name' => $item['operuserName'] ?? "",
                            'so_id' => $processingCode ?: $remark,
                            'io_date' => $ioDate,
                            'wh_id' => $wh_id,
                            'wms_co_id' => $item['warehouseCode'] ?? "",
                            'warehouse' => $item['goodsOwner'] ?? "",
                            'sku_id' => $skuId,
                            'qty' => $qty,
                            'ke_date' => $ioDate,
                            'processing_code' => $processingCode ?: $remark,
                            'createtime'=>date("Y-m-d H:i:s")
                        ];
                        $res = (new SsyJstPurchaseinOrderTable())->createData($data);
                        //七月夕的回货入库数 推送领猫
                        if($data['warehouse'] == 'QYX' && ($res['id'] ?? "")){
                            $data['id'] = $res['id'];
                            (new LinmaoRepository())->pushOrder($data);
                        }
                    }
                }catch (\Exception $e){
                    info("巨沃回货数", [$e->getMessage(), $item]);
                    dd($e->getMessage(), $item);
                }
            }
            $num++;
        }
    }
}
